package com.aneliya.application;

import com.aneliya.command.application.StartApplicationCommand;
import com.aneliya.framework.mvc.Facade;
import com.aneliya.framework.mvc.controller.MVCEvent;
import com.aneliya.mediator.ChatWindowMediator;
import com.aneliya.mediator.LoginDialogMediator;
import com.aneliya.proxy.MessagesProxy;
import com.aneliya.proxy.OnlineUsersProxy;
import com.aneliya.proxy.ServerProxy;
import com.aneliya.proxy.UserProxy;

public class ChatApplication {
	public static final String START_APPLICATION_EVENT = "startApplicationEvent";

	private Facade facade;

	public ChatApplication() throws Exception {
		facade = Facade.getInstance();

		initializeMediators();
		initializeProxies();
		initializeCommands();
	}

	private void initializeMediators() throws Exception {
		ChatWindowMediator chatWindowMediator = new ChatWindowMediator();
		facade.registerMediator(chatWindowMediator, chatWindowMediator.name);

		LoginDialogMediator loginDialogMediator = new LoginDialogMediator();
		facade.registerMediator(loginDialogMediator, loginDialogMediator.name);
	}

	private void initializeProxies() throws Exception {
		UserProxy userProxy = new UserProxy();
		facade.registerProxy(userProxy, userProxy.name);

		MessagesProxy messagesProxy = new MessagesProxy();
		facade.registerProxy(messagesProxy, messagesProxy.name);

		OnlineUsersProxy onlineUsersProxy = new OnlineUsersProxy();
		facade.registerProxy(onlineUsersProxy, onlineUsersProxy.name);
		
		ServerProxy serverProxy = new ServerProxy();
		facade.registerProxy(serverProxy, serverProxy.name);
		
	}

	private void initializeCommands() throws Exception {
		facade.registerCommand(START_APPLICATION_EVENT,
				StartApplicationCommand.class);
	}

	public void start() throws InstantiationException, IllegalAccessException {
		facade.dispatchMVCEvent(new MVCEvent(this, START_APPLICATION_EVENT,
				null));
	}

	public static void main(String[] args) throws Exception {
		ChatApplication chatApplication = new ChatApplication();
		chatApplication.start();
	}

}
